package com.xyy.yueshijia.myadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xyy.yueshijia.R;
import com.xyy.yueshijia.bean.MyListBean;

import java.util.List;

/**
 * Created by Asus on 2017/2/8.
 */

public class MyListaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private LayoutInflater inflater;
    private Context context;
    private List<MyListBean> lists;
    public MyListaAdapter(Context context, List<MyListBean> lists) {
        this.context = context;
        this.lists=lists;
        inflater = LayoutInflater.from(context);
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , String data);
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= inflater.inflate(R.layout.homepagefragment_item1, parent, false);
        view.setOnClickListener(this);
        return new YHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof YHolder) {
            YHolder tHolder = (YHolder) holder;
            MyListBean  btn=lists.get(position);
            tHolder.tvTitle.setText(btn.getRelation_object_title());
            tHolder.imageView.setImageResource(R.drawable.bg_morentu);
            tHolder.itemView.setTag(btn.getRelation_object_title());
            String imageURI=btn.getRelation_index_img();

            Glide.with(context)
                    .load(imageURI)
                    .placeholder(R.drawable.bg_morentu)
                    // .bitmapTransform(transformation) //转换
                    .error(R.drawable.bg_morentu)
                    .into(tHolder.imageView);
        }
    }

    @Override
    public int getItemCount() {
        return lists == null ? 0 : lists.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(String)v.getTag());
        }


    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    class YHolder extends RecyclerView.ViewHolder{

        TextView tvTitle;
        ImageView imageView;

        public YHolder(View itemView) {
            super(itemView);
            tvTitle=(TextView) itemView.findViewById(R.id.hometitle);
            imageView=(ImageView) itemView.findViewById(R.id.homeicon);

        }
    }


}
